package com.kang.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectListing;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kang.service.OssOperService;
import com.kang.util.OssPropertiesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class OssOperServiceImpl implements OssOperService {


    @Override
    public Map<String,Object> getOssShiPinList(Integer page, Integer limit) {
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(OssPropertiesUtils.ALIYUN_OSS_ENDPOINT, OssPropertiesUtils.ALIYUN_OSS_ACCESSKEYID, OssPropertiesUtils.ALIYUN_OSS_SECRET);
            // 列举文件。
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(OssPropertiesUtils.ALIYUN_OSS_BUCKET);
            //获取指定目录下的文件
            listObjectsRequest.setDelimiter("/");
            listObjectsRequest.setPrefix("shipin/");
            ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            Map<String, Object> resultMap = new HashMap<>();
            resultMap.put("total",sums.size());
            List<OSSObjectSummary> subList = null;
            if ((page - 1) * limit + limit >= sums.size()) {
                subList = sums.subList((page - 1) * limit,sums.size());
            } else {
                subList = sums.subList((page - 1) * limit, (page - 1) * limit + limit);
            }
            subList.forEach(v -> {
                v.setKey("https://moyou-yuhongkang.oss-cn-hangzhou.aliyuncs.com/" + v.getKey());
            });
            resultMap.put("data",subList);
            resultMap.put("pageNum",page);
            resultMap.put("pageSize",limit);
            return resultMap;
        } catch (Exception ex) {
            log.error("查询oss视频文件异常",ex);
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public Map<String, Object> getOssTuPianList(Integer page, Integer limit) {
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(OssPropertiesUtils.ALIYUN_OSS_ENDPOINT, OssPropertiesUtils.ALIYUN_OSS_ACCESSKEYID, OssPropertiesUtils.ALIYUN_OSS_SECRET);
            // 列举文件。
            ListObjectsRequest listObjectsRequest = new ListObjectsRequest(OssPropertiesUtils.ALIYUN_OSS_BUCKET);
            //获取指定目录下的文件
            listObjectsRequest.setDelimiter("/");
            listObjectsRequest.setPrefix("tupian/");
            ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
            List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
            Map<String, Object> resultMap = new HashMap<>();
            List<OSSObjectSummary> subList = null;
            if ((page - 1) * limit + limit >= sums.size()) {
                subList = sums.subList((page - 1) * limit,sums.size());
            } else {
                subList = sums.subList((page - 1) * limit, (page - 1) * limit + limit);
            }
            subList.forEach(v -> {
                v.setKey("https://moyou-yuhongkang.oss-cn-hangzhou.aliyuncs.com/" + v.getKey());
            });
            resultMap.put("data",subList);
            resultMap.put("pageNum",page);
            resultMap.put("pageSize",limit);
            return resultMap;
        } catch (Exception ex) {
            log.error("查询oss图片文件异常",ex);
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

    @Override
    public Integer deleteOssTuPianLis(String fileName) {
        List<String> splitFileName = Arrays.stream(fileName.split("/")).collect(Collectors.toList());
        fileName = splitFileName.get(3) + "/" + splitFileName.get(4);
//        splitFileName.get(4).split()
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(OssPropertiesUtils.ALIYUN_OSS_ENDPOINT, OssPropertiesUtils.ALIYUN_OSS_ACCESSKEYID, OssPropertiesUtils.ALIYUN_OSS_SECRET);
            ossClient.deleteObject(OssPropertiesUtils.ALIYUN_OSS_BUCKET, fileName);
            return 1;
        } catch (Exception ex) {
            log.error("删除oss图片文件异常",ex);
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

    }

    @Override
    public Integer deleteOssShiPinLis(String fileName) {
        List<String> splitFileName = Arrays.stream(fileName.split("/")).collect(Collectors.toList());
        fileName = splitFileName.get(3) + "/" + splitFileName.get(4);
        OSS ossClient = null;
        try {
            ossClient = new OSSClientBuilder().build(OssPropertiesUtils.ALIYUN_OSS_ENDPOINT, OssPropertiesUtils.ALIYUN_OSS_ACCESSKEYID, OssPropertiesUtils.ALIYUN_OSS_SECRET);
            ossClient.deleteObject(OssPropertiesUtils.ALIYUN_OSS_BUCKET, fileName);
            return 1;
        } catch (Exception ex) {
            log.error("删除oss视频文件异常",ex);
            return null;
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
}
